<?php

/**
 * 支付中心（Payment）SDK
 * @author iJoggers.com
 */
class Payment
{

    /**
     * 应用ID
     * @var integer
     */
    private $appid = 10001;

    /**
     * 应用Key
     * @var string
     */
    private $appkey = 'a84d6f67dd186d3f50709e6cd11b9700';

    /**
     * SDK版本
     * @var integer
     */
    private $version = 1000;

    /**
     * 接口地址
     * @var string
     */
    private $api = 'https://ijoggers.com/payment';

    /**
     * 获取接口内容
     * @param string $apiName 接口名称
     * @param array $data 接口参数
     * @param integer $timeout 请求超时时间（秒）
     * @return array
     */
    public function getApi($apiName, array $data = array(), $timeout = 5)
    {
        $result = NULL;

        $param = $this->initParam($data);
        $param['sign'] = $this->sign($param);

        $result = $this->getContents($this->api . '/' . $apiName, $param, 5, 'POST');
        $result && $result = json_decode($result, TRUE);

        !$result && $result = [];
        return $result;
    }

    /**
     * 验证签名
     * @param array $data 完事回调参数
     * @return boolean
     */
    public function verifySign(array $data)
    {
        $verify = FALSE;

        $serverSign = $data['sign'];

        $param = $this->initParam($data);
        $sign = $this->sign($param);

        $verify = $serverSign == $sign;

        return $verify;
    }

    /**
     * 获取签名
     * @param array $data 接口参数
     * @return string
     */
    public function getSign(array $data)
    {
        $sign = NULL;

        $param = $this->initParam($data);
        $sign = $this->sign($param);

        return $sign;
    }

    /**
     * 初始化接口参数
     * @param array $data 接口参数
     * @var array
     */
    private function initParam(array $data = array())
    {
        $param = $data;

        $param['appid'] = $this->appid;
        $param['appkey'] = $this->appkey;
        $param['version'] = $this->version;

        $param = $this->paramSort($param);

        return $param;
    }

    /**
     * 参数排序
     * @param array $param
     * @return array
     */
    private function paramSort(array $param)
    {
        $param && ksort($param) && reset($param);

        return $param;
    }

    /**
     * 获取签名
     * @param string $param
     * @return string
     */
    private function sign($param)
    {
        foreach($param as $key => $value)
        {
            if(in_array($key, array('appkey', 'sign')))
            {
                unset($param[$key]);
            }
        }

        return md5(md5('iJoggers' . md5(http_build_query($param, NULL, '&'))) . '.com');
    }

    /**
     * CURL获取远程内容
     * @param string $url
     * @param array $datas
     * @param int $timeout
     * @param string $method
     * @return string
     */
    private function getContents($url, array $datas = array(), $timeout = 10, $method = 'GET', $contentType = NULL, $cookies = NULL)
    {
        $method = strtoupper($method);

        $query = is_array($datas) ? http_build_query($datas, NULL, '&') : $datas;

        if ($method == 'GET' && !empty($query))
        {
            $url .= '?' . $query;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        if ($method == 'POST' && !empty($query))
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        }

        if(stripos($url,"https://")!==FALSE)
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }

        !empty($cookies) && curl_setopt($ch, CURLOPT_COOKIE, $cookies);

        if ($contentType)
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType));
        }

        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $results = curl_exec($ch);

        curl_close($ch);

        return $results;
    }

}